package cn.lutu.lutu.service.impl;

import cn.lutu.lutu.config.AliPayConfig;
import cn.lutu.lutu.pojo.entity.AlipayOrder;
import cn.lutu.lutu.service.AlipayOrderService;
import cn.lutu.lutu.service.AlipayService;
import cn.lutu.lutu.utils.SysConstant;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 阿里支付服务
 */
@Service
@Slf4j
public class AlipayServiceImpl implements AlipayService {
  @Autowired
  private AlipayOrderService alipayOrderService;

  @Autowired
  AliPayConfig aliPayConfig;
    @Override
    public String createTrade() {
        //1.创建订单
        log.info("调用创建订单服务");
        AlipayOrder alipayOrder = alipayOrderService.createOrder();
        //2.获取参数
        //appid
        String app_id=aliPayConfig.app_id;
        //应用私钥
        String merchant_private_key=aliPayConfig.merchant_private_key;
        //支付宝公钥
        String alipay_public_key=aliPayConfig.alipay_public_key;
        //异步通知
        String notify_url=aliPayConfig.notify_url;
        //同步通知
        String return_url=aliPayConfig.return_url;
        //签名类型
        String sign_type=aliPayConfig.sign_type;
        //字符编码
        String charset=aliPayConfig.charset;
        //支付宝网关
        String gatewatUrl=aliPayConfig.gatewatUrl;

        //3.获取支付客户端对象
        AlipayClient alipayClient=new DefaultAlipayClient(gatewatUrl,app_id,merchant_private_key, SysConstant.DATA_TYPE,charset,alipay_public_key,sign_type);

        //4.创建支付请求对象
        AlipayTradePagePayRequest request=new AlipayTradePagePayRequest();
        //设置异步通知路径
        request.setNotifyUrl(notify_url);
        //设置同步通知路径
        request.setReturnUrl(return_url);
        //组装biz_content请求参数合集
        JSONObject biz_content=new JSONObject();
        //组装订单号
        biz_content.put("out_trade_no", alipayOrder.getOrderNo());
        //组装金额
        biz_content.put("total_amount", alipayOrder.getTotalAmount());
        //组装标题
        biz_content.put("subject", alipayOrder.getSubject());
        //组装产品码
        biz_content.put("product_code","FAST_INSTANT_TRADE_PAY");

        request.setBizContent(String.valueOf(biz_content));

         //调用远程支付宝接口
        AlipayTradePagePayResponse response;
        try {
           response= alipayClient.pageExecute(request);
           if (response.isSuccess()){
               log.info("调用成功");
           }else {
               log.info("调用失败失败");
           }
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        return response.getBody();
    }

    /**
     *验签名方法
     * @param params
     * @return
     */
    @Override
    public boolean aliSignature(Map<String, String> params) throws AlipayApiException {
       boolean signVerified= AlipaySignature.rsaCheckV1(params,aliPayConfig.getAlipay_public_key(),aliPayConfig.getCharset(),aliPayConfig.getSign_type());
        if(!signVerified){
            return false;
        }
        System.out.println(true);
        return true;
    }
}
